App.controller('refundController', ['$rootScope', '$scope', '$state', 'Page', 'XCode',function($rootScope, $scope, $state, Page, Xode) {

    $scope.refund = {};
    //$scope.refundDetail = {};

    function isRepeat(refundDetails){
        var hash = {};
        for(var i in refundDetails) {
            if(hash[refundDetails[i].minutes])
                return true;
            hash[refundDetails[i].minutes] = true;
        }
        return false;
    }

    $scope.grid = {
        add: function () {
            $scope.type = 1;
            $scope.refund = {refundDetails: []};
            $scope.refund.deleteDetailList = [];
            $scope.modal.show();
        },
        addDetail: function() {
            $scope.refund.refundDetails.push({});
        },
        deleteDetail: function(index) {
            //不进行数据库删除，记录需要删除的明细
            var obj = $scope.refund.refundDetails[index];
            if(obj.refundDetailId != undefined && obj.refundDetailId != "") {
                $scope.refund.deleteDetailList.push(obj);
                $scope.refund.refundDetails.splice(index, 1);
            } else {
                $scope.refund.refundDetails.splice(index, 1);
            }
        },
        save: function() {

            for (var i = 0; i < $scope.refund.refundDetails.length; i++) {
                var obj = $scope.refund.refundDetails[i];
                if($scope.refund.refundType == '1') { //发车前时间折扣率
                    if(obj.minutes == undefined || obj.minutes == ''
                        || obj.refundRate == undefined || obj.refundRate == ''
                        || obj.refundDetailStatus == undefined || obj.refundDetailStatus == '') {
                        Page.notice("请填写必填项!");
                        return;
                    } else {
                        var x = new RegExp("^[0-9]*[1-9][0-9]*$");
                        if(!x.test(obj.minutes)) {
                            Page.notice("请填写正确的提前时间!");
                            return;
                        }

                        if(!(obj.refundRate > 0 && obj.refundRate < 1)) {
                            Page.notice("请填写正确的折扣率(0-1之间)");
                            return;
                        }
                    }
                } else { //固定费率
                    if(obj.fixedFee == undefined || obj.fixedFee == ''
                        || obj.refundDetailStatus == undefined || obj.refundDetailStatus == '') {
                        Page.notice("请填写必填项!");
                        return;
                    } else {
                        if(obj.fixedFee < 0){
                            Page.notice("请填写正确的固定费率");
                            return;
                        }
                    }
                }

            }

            if($scope.refund.refundType == '1') { //发车前时间折扣率
                if(isRepeat($scope.refund.refundDetails)) {
                    Page.notice("提前时间重复!");
                    return;
                }
            }



            Page.ajaxPost($scope.refund.refundId ? 'refund/update.do' : 'refund/add.do', $scope.refund, function() {
                $scope.grid.query();
                $scope.grid.cancel();
            });
        },
        column_defs: [{
            target: 5,
            buttons: [
                {
                    name: '查看',
                    class: 'btn-success btm-sm',
                    icon: 'fa-search',
                    event: function(row) {
                        $scope.refund = row;
                        $scope.type = 3;
                        $scope.modal.show();
                    }
                },
                {
                    name: '编辑',
                    class: 'btn-warning btm-sm',
                    icon: 'fa-edit',
                    event: function(row) {
                        $scope.refund = row;
                        $scope.refund.deleteDetailList = [];
                        $scope.type = 2;
                        $scope.modal.show();
                    }
                },
                {
                    name: '删除',
                    class: 'btn-danger',
                    icon: 'fa-close',
                    event: function(row) {
                        $scope.refund = row;
                        $scope.refund.refundStatus = '0';
                        Page.confirm("确定要删除吗？", function() {
                            Page.ajaxPost('refund/delete.do', $scope.refund, function() {
                                $scope.grid.query();
                            });
                        });
                    }
                }
            ]
        }]
    };

    $scope.detailGrid = {
        add: function () {
            $scope.typeDetail = 1;
            $scope.refundDetail = {};
            $scope.detailModal.show();
        },
        save: function() {
            $scope.refundDetail.refundId = $scope.refund.refundId;
            Page.ajaxPost($scope.refundDetail.refundDetailId ? 'refundDetail/update.do' : 'refundDetail/add.do', $scope.refundDetail, function() {
                $scope.detailGrid.query();
                $scope.detailGrid.cancel();
            });
        },
        options: {
            paging: false
        },
        column_defs: [{
            target: 5,
            buttons: [
                {
                    name: '编辑',
                    class: 'btn-warning btm-sm',
                    icon: 'fa-edit',
                    event: function(row) {
                        $scope.refundDetail = row;
                        $scope.typeDetail = 2;
                        $scope.detailModal.show();
                    }
                },
                {
                    name: '删除',
                    class: 'btn-danger',
                    icon: 'fa-close',
                    event: function(row) {
                        $scope.refundDetail = row;
                        $scope.refundDetail.refundDetailStatus = '0';
                        Page.confirm("确定要删除吗？", function() {
                            Page.ajaxPost('refundDetail/update.do', $scope.refundDetail, function() {
                                $scope.detailGrid.query();
                            });
                        });
                    }
                }
            ]
        }]
    };

}]);